;(function($){
	/**
	 *餐饮帐单插件
	 */
	$.fn.ct_bill = function(opt){
		var el = this;
		var opts = {
			as_json : "{arg:{}}",
			as_json2 : "{}",
			as_json3 : "",
			args : "3",// 2 : 存储过程调用2个参数 ； 3 : 存储过程调用3个参数 
			report_id : "",
			nos : "",
			arg : {},
			close : false,
			ls_param : {"as_json":'{"args":{"billtype":"重印单","gledgerall":"","retrieve":"gno","where_no":"","coid":"0","win":"1","gno":"41728"}}'},
			dbsql : "2", //
			gno : 0,
			billType : "RC",
			JSESSIONID : "",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opts,opt);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
//		//初始打印参数
//		this.get_ls_param();
		//初始样式
		this.initBillType_pos();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭模态
			el.find("#close-dialog,#modalBtnCancel").unbind("click").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			})
			
			//查询打印
			this.el.find("#btnSearchPrint").unbind("click").click(function(){
				
				var ls_parameter = "report_id="+param.report_id;
				var frmJson = {};
				frmJson = param.ls_param;
				frmJson.dbsql = param.dbsql;
				
				var session = {"userid":localStorage.getItem("userid"),
					"username":localStorage.getItem("username"),
					"netcard":localStorage.getItem("netcard"),
					"ip":localStorage.getItem("ip")};
				
				if(frmJson.dbsql==2){
					var js1 = $.parseJSON(frmJson.as_json);
					js1.session = session;
					frmJson.as_json = JSON.stringify(js1);
					frmJson = $.extend({},frmJson);
				}
				
				for(var key in frmJson){
					ls_parameter += "&" + key + "="+frmJson[key];
				}
				// var url = window.location.protocol+
				// 		"//"+window.location.host+$.HSAPI_AJAXURL+"/hobill/bill?JSESSIONID="+
				// 		param.JSESSIONID+"&hotelId="+(localStorage.getItem("hotelId")||jf_getHotelId())+"&"+ls_parameter+"&_modify_";
				// url=url.replace(window.location.protocol+"//","hsrpt:")
				// url=url

				var url = $.HSAPI_BILLURL+"&hotelId="+$.HSAPI_HEADERS.hotelId+"&"+ls_parameter+"&_modify_";
				console.log(url);
				window.location.href = encodeURI(url);
			});
			
			//关闭打印
			el.find("#btnClosePrint").unbind("click").click(function(){
				el.modal("hide");
				el.find("#btnSearchPrint").trigger("click");
				el.empty();
				param.closeBack();
			})
			this.el.find("#btnSearchPrint").trigger("click");
		},
		initHtml : function(){
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="modal-dialog" style="width:800px;top:50px;">'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white bigger-150">&times;</span>'+
										'</button>'+
										'<span id="title-dialog" style="font-size:25px;font-weight:bold;">'+$.i18n.propJsp("帐单")+'</span>'+
										'<i class="ace-icon fa fa-angle-double-right"></i>'+
										'<span id="ins_argjson_some">NO:'+this.param.no+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="height:220px;">'+
									'<div id="hsradiogrid" style="display: -webkit-box;"></div>'+
									'<div class="well well-sm">'+
										'<span id="billList" data-toggle="buttons" class="btn-group no-margin"></span>'+
									'</div>'+
									'<div class="widget-box">'+
										'<div class="widget-header">'+
											'<h5 class="widget-title">'+$.i18n.propJsp("帐单过滤条件")+'</h5>'+
										'</div>'+
										'<div class="widget-body">'+
											'<div id="billArgs" style ="margin-left:10px;margin-top:10px;margin-bottom:10px;"></div>'+
										'</div>'+
									'</div>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="btn-group">'+
										'<button id="btnEditBill" class="btn btn-info hide">'+
											'<i clas="ace-icon fa fa-eye"></i>'+$.i18n.propJsp("修改帐单")+
										'</button>'+
									'</div>'+
									'<div class="btn-group">'+
										'<button id="btnClosePrint" class="btn btn-info">'+
											'<i clas="ace-icon fa fa-eye"></i>'+$.i18n.propJsp("关闭打印")+
										'</button>'+
									'</div>'+
									'<div class="btn-group">'+
										'<button id="btnSearchPrint" class="btn btn-info">'+
											'<i clas="ace-icon fa fa-eye"></i>'+$.i18n.propJsp("查询打印")+
										'</button>'+
									'</div>'+
									'<div class="btn-group">'+
										'<button id="modalBtnCancel" type="button" class="btn btn-default">'+
											'<i class="ace-icon fa fa-undo"></i>'+
											$.i18n.propJsp("退出")+
										'</button>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			this.el.html(html);
		},
		setInitValue : function(){//添加帐单HTML
			
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var args = '';
			args = "账单编号 : " + '<span style="color:green;">' + this.param.report_id +'</span>';
			var ls = this.param.ls_param;
			for(var k in ls){
				args += "  " + k + " : " + '<span style="color:green;">'+ls[k]+'</span>'; 
			}
			this.el.find("#billArgs").html(args);
			
			//帐单类型点击
			this.el.find("#billList label").unbind("click");
			this.el.find("#billList label").bind("click",function(){
				el.find("#billList label").removeClass("active");
				$(this).addClass("active");
				param.report_id = $(this).find("input").data("id");
				t.setInitValue();
			})
			
			//选择账单类型
			el.find("#grid-logo-wrap").unbind("click");
			el.find(".grid-logo-wrap").click(function(){
				param.report_id = $(this).data("code");
				$(this).css({"pointer-events":"none"});
				t.initBillType_pos();
//				t.setInitValue();
			})
		},
		initBillType_pos : function(){
			var param = this.param;
			var el = this.el;
			var t = this;
			t.getDs(function(ds){
				var sortDs = sortByKey(ds.reportlist, "report_id")
				var option = "";
				$(sortDs).each(function(k,v){
					var str = "";
					if(v.report_id==param.report_id.toString().substr(0,3)){
						str = "selected"
					}
					option += 	'<label title="'+v.report_name+'" data-code="'+v.report_id+'" class="grid-logo-wrap '+str+'">'+
							  		'<i class="icon-radio"></i>'+v.report_name+'<i class="icon-radio"></i>'+
							  	'</label>';
				})
				var html = 	'<div class="hs-radio-grid">'+
								option+
							'</div>';
				el.find("#hsradiogrid").html(html);
				
				
				var html = "";
				var report_id = param.report_id;
				$(ds.style).each(function(k,v){
					var active = "";
					if(v.report_id==report_id){
						active = "active";
					}
					html += '<label class="btn btn-sm btn-yellow '+active+'">'+
								'<input type="radio" name="lStyle" data-id="'+v.report_id+'" data-name="'+v.report_name+'">'+v.report_name+
							'</label>';
				});
				
				el.find("#billList").html(html);
				
				//赋值
				t.setInitValue();
				//绑定事件
				t.bindEvent();
				if(param.close){
					el.modal("hide");
					el.empty();
					param.closeBack();
				}
			});
			
		},
		getDs: function(back){
			var param = this.param;
			var json = null;
			var t = this;
			var el = this.el;
			var askData = "";
			var url = "";
			//根据条件选择需要的存储过程
			param.as_json = {arg:{"no":param.no,"nos":param.nos,xhs:param.xhs}}
			param.as_json.arg = $.extend(param.arg,param.as_json.arg);//合成-10的参数
			param.as_json["arg"] = $.extend(param.as_json["arg"],{"billtype":this.param.billType,"report_id":(this.param.report_id.toString().substr(0,3)||"")})
			askData = $.HSAPI_ASKJSONINFO(JSON.stringify(param.as_json),'HoBILLS_BillDefault3',param.as_json2,param.as_json3) 
			url = $.HSAPI_AJAXURL +"/qms/mobile/procedure3",
			
			jQuery.ajax({  
				type : 'POST',
				async:false, 
				url : url,  
				data : askData,
				dataType : 'json',  
				success : function(data){
					if(param.report_id==""){
						param.report_id = data.report_id||"";
					}
		            if(data.RETURN == 1){
		            	json = data
		            	if(data.istemp=="YES"){
		            		param.ls_param = {temp_id:data.temp_id};
		    				param.dbsql=1;
		            	}
		            	back(json);
		            }else if(data.RETURN==-10){
		            	t.errorContent(back,data)
		            }else{
		            	jf_alert(data.MSG)
		            	back(json)
					}
				},  
				error : function(data){ 
					systoast('ExecRetrieveJson',("提交错误") + JSON.stringify(data),'error')
					back(json)
				}  
			});
//			return json;
		},
		errorContent:function(back,data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var MSG = data.MSG;
			var name = data.checkbox;
			var value = (data.checkboxvalue||"");
			
			var checked = "";
			if(value==1){
				checked = "checked"
			}
			
			var html = 	'<h3 class="header smaller lighter blue">'+
							'<i class="ace-icon fa fa-bullhorn"></i>'+MSG+
						'</h3>'+
						'<div class="alert alert-block alert-danger">'+
							'<label>'+
								'<input id="checkin_cccheckbox" type="checkbox" name="'+name+'" value="" class="ace" '+checked+'/>'+
								'<span class="lbl" style="color:#478fca;">'+MSG+'</span>'+
							'</label>'+
						'</div>';

			bootbox.dialog({
				message: html,
				buttons:{
					"button1":{
						"label" : $.i18n.propJsp("确定"),
						"className" : "btn-sm btn-info",
						"callback" : function(){
							if($("#checkin_cccheckbox").prop("checked")){
								param.arg[name]="yes";
								t.getDs(back);
							}else{
								param.arg[name]="no";
								t.getDs(back);
							}
						}
					},	
					"button" :
					{
						"label" : $.i18n.propJsp("取消"),
						"className" : "btn-sm",
						"callback" : function(){
							el.modal("hide");
							el.empty();
							param.closeBack();
						}
					},
				}
			});
		},
//		get_ls_param : function(){
//			var el = this.el;
//			var param = this.param;
//			var t = this;
//			var arg = {"arg":{"no":param.no,"nos":param.nos,xhs:param.xhs}}
//			jQuery.ajax({  
//				type : 'POST',
//				async:false, 
//				url :  getContextPath() + "/pos/front/hoguest/procedure2",
//				data : $.HSAPI_ASKJSONINFO(JSON.stringify(arg),'POSBILLS_BillFolio','{}'),
//				dataType : 'json',  
//				success : function(data){
//					if(data.RETURN==1){
//						var js = {temp_id:data.temp_id};
//						param.ls_param = $.extend(param.ls_param,js); 
//					}else{
//						jf_alert(data.MSG)
//					}
//				},  
//				error : function(data){ 
//					systoast('POSBILLS_BillFolio',("提交错误") + JSON.stringify(data),'error')
//				}  
//			});
//		},
	}
	
	//排序
	function sortByKey(array, key) {
	    return array.sort(function(a, b) {
	        var x = a[key]; var y = b[key];
	        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	    });
	}
})(jQuery)